package com.ruoyi.social.service.impl;

import java.util.*;
import java.util.stream.Collectors;

import com.ruoyi.social.domain.SocialKolUser;
import com.ruoyi.social.domain.SocialProduct;
import com.ruoyi.social.service.ISocialKolUserService;
import org.springframework.beans.factory.annotation.Autowired;
import org.springframework.stereotype.Service;
import com.ruoyi.social.mapper.SocialKolMapper;
import com.ruoyi.social.domain.SocialKol;
import com.ruoyi.social.service.ISocialKolService;
import org.springframework.transaction.annotation.Transactional;
import org.springframework.util.CollectionUtils;

/**
 * KOLService业务层处理
 * 
 * @author ruoyi
 * @date 2025-09-24
 */
@Service
public class SocialKolServiceImpl implements ISocialKolService 
{
    @Autowired
    private SocialKolMapper socialKolMapper;

    @Autowired
    private ISocialKolUserService kolUserService;

    /**
     * 查询KOL
     * 
     * @param id KOL主键
     * @return KOL
     */
    @Override
    public SocialKol selectSocialKolById(Long id)
    {
        return socialKolMapper.selectSocialKolById(id);
    }

    /**
     * 查询KOL列表
     * 
     * @param socialKol KOL
     * @return KOL
     */
    @Override
    public List<SocialKol> selectSocialKolList(SocialKol socialKol)
    {
        return socialKolMapper.selectSocialKolList(socialKol);
    }

    @Override
    @Transactional
    public int updateKolList(Long userId, List<SocialKol> kolList, SocialProduct product) {
        if (CollectionUtils.isEmpty(kolList)) {
            return 0;
        }
        kolList = kolList.stream()
                .filter(Objects::nonNull)
                .collect(Collectors.collectingAndThen(
                        Collectors.toCollection(() ->
                                new TreeSet<>(Comparator.comparing(SocialKol::getKolOutid))
                        ),
                        ArrayList::new
                ));
        // 1. 批量查询已存在的KOL
        List<String> outIds = kolList.stream()
                .map(SocialKol::getKolOutid)
                .collect(Collectors.toList());
        List<SocialKol> existingVideos = socialKolMapper.selectSocialKolByOutIds(outIds);
        Map<String, Long> outIdToIdMap = existingVideos.stream()
                .collect(Collectors.toMap(SocialKol::getKolOutid, SocialKol::getId));

        // 2. 分离更新和插入的数据
        List<SocialKol> toUpdate = new ArrayList<>();
        List<SocialKol> toInsert = new ArrayList<>();

        for (SocialKol kol : kolList) {
            Long existingId = outIdToIdMap.get(kol.getKolOutid());

            if (existingId != null) {
                kol.setId(existingId);
                toUpdate.add(kol);
            } else {
                toInsert.add(kol);
            }
        }

        // 3. 批量操作
        int updateCount = 0;
        int insertCount = 0;

        if (!toUpdate.isEmpty()) {
            for(SocialKol kol: toUpdate) {
                updateCount += updateSocialKol(kol);
            }
        }

        if (!toInsert.isEmpty()) {
            // 插入改为单条插入 方便添加关联
            insertCount = socialKolMapper.batchInsertSocialKol(toInsert);
        }
        List<Long> ids = socialKolMapper.getIdListByOutIds(outIds);
        if(!ids.isEmpty()) {
            kolUserService.batchInsertSocialKolUser(userId, ids);
        }

        return updateCount + insertCount;
    }

    /**
     * 新增KOL
     * 
     * @param socialKol KOL
     * @return 结果
     */
    @Override
    public int insertSocialKol(SocialKol socialKol)
    {
        return socialKolMapper.insertSocialKol(socialKol);
    }

    /**
     * 修改KOL
     * 
     * @param socialKol KOL
     * @return 结果
     */
    @Override
    public int updateSocialKol(SocialKol socialKol)
    {
        return socialKolMapper.updateSocialKol(socialKol);
    }

    /**
     * 批量删除KOL
     * 
     * @param ids 需要删除的KOL主键
     * @return 结果
     */
    @Override
    public int deleteSocialKolByIds(Long[] ids)
    {
        return socialKolMapper.deleteSocialKolByIds(ids);
    }

    /**
     * 删除KOL信息
     * 
     * @param id KOL主键
     * @return 结果
     */
    @Override
    public int deleteSocialKolById(Long id)
    {
        return socialKolMapper.deleteSocialKolById(id);
    }
}
